package com.example.musicplayer

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
import com.example.musicplayer.model.SongCategory

class SongCategoryAdapter(private val categories: List<SongCategory>, private val itemClickListener: OnItemClickListener) :
    RecyclerView.Adapter<SongCategoryAdapter.CategoryViewHolder>() {

    interface OnItemClickListener {
        fun onItemClick(category: SongCategory)
    }

    class CategoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val button: Button = itemView.findViewById(R.id.category_button)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_category, parent, false)
        return CategoryViewHolder(view)
    }

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        val category = categories[position]
        holder.button.text = category.name
        holder.button.setOnClickListener {
            itemClickListener.onItemClick(category)
        }
    }

    override fun getItemCount(): Int {
        return categories.size
    }
}